Skip to content

Fix #2266: Do not replace constant type lazy vals with constant. #2267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 19, 2017

Conversation

nicolasstucki
Copy link
Contributor

No description provided.

@@ -427,7 +427,7 @@ trait TypedTreeInfo extends TreeInfo[Type] { self: Trees.Instance[Type] =>
def constToLiteral(tree: Tree)(implicit ctx: Context): Tree = {
val tree1 = ConstFold(tree)
tree1.tpe.widenTermRefExpr match {
case ConstantType(value) if isIdempotentExpr(tree1) => Literal(value)
case ConstantType(value) if isIdempotentExpr(tree1) && !tree1.symbol.is(Lazy) => Literal(value)
Copy link
Contributor

@DarkDimius DarkDimius Apr 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is specific to lazy vals.

abstract class A {
  def s: Boolean = r
  def r: Boolean
  assert(Test.r == this.s)
}

object Test extends A {
   override val r: true = true
   def main(args: Array[String]): Unit = {}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// updated example to make it more likely to trigger

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the issue you would expect with that example?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assertion will fail, while it shouldn't.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, forgot to update the example, though I've left the comment that I did update it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now, updated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, that one fails for a similar reason.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a fix for that issue.

// lazy value must be initialized (would not be needed with isPureExpr)
!tree1.symbol.is(Lazy) &&
// could hide initialization order issues (ex. val with constant type read before initialized)
(!ctx.owner.isLocalDummy || (tree1.symbol.is(Method) || value.isZero) ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tree1.symbol.is(Method)

Why is inlining the return value of a (possibly-side-effectfull) method safe?

@nicolasstucki nicolasstucki force-pushed the fix-constant-lazy-vals branch from 57942db to 983ce8d Compare April 17, 2017 15:40
@DarkDimius
Copy link
Contributor

LGTM

@DarkDimius DarkDimius merged commit 02e3883 into scala:master Apr 19, 2017
@allanrenucci allanrenucci deleted the fix-constant-lazy-vals branch December 14, 2017 19:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants